<?php
// 更新oa_admin_rule表的mig_src字段

// 引入数据库配置
require_once '../config/database.php';

// 获取数据库配置
$config = include '../config/database.php';
$dbConfig = $config['connections']['mysql'];

try {
    // 创建PDO连接
    $dsn = "mysql:host={$dbConfig['hostname']};port={$dbConfig['hostport']};dbname={$dbConfig['database']};charset={$dbConfig['charset']}";
    $pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password'], [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ]);
    
    // 查询oa_admin_rule表所有记录
    $stmt = $pdo->query("SELECT * FROM oa_admin_rule");
    $records = $stmt->fetchAll();
    
    // 路由映射表（根据实际情况补充）
    $routeMap = [
        'oa/plan' => '/oa/plan',
        'oa/schedule' => '/oa/schedule',
        'oa/work' => '/oa/work',
        // 可以根据需要添加更多映射
    ];
    
    // 更新每条记录的mig_src字段
    foreach ($records as $record) {
        $src = $record['src'];
        $id = $record['id'];
        
        // 根据src找到对应的路由
        $migSrc = '';
        if (isset($routeMap[$src])) {
            $migSrc = $routeMap[$src];
        }
        
        // 更新数据库
        if (!empty($migSrc)) {
            $updateStmt = $pdo->prepare("UPDATE oa_admin_rule SET mig_src = ? WHERE id = ?");
            $updateStmt->execute([$migSrc, $id]);
            echo "已更新ID {$id} 的mig_src为 {$migSrc}\n";
        } else {
            echo "未找到ID {$id} (src: {$src}) 对应的路由\n";
        }
    }
    
    echo "mig_src字段更新完成\n";
    
} catch (PDOException $e) {
    echo "数据库操作失败: " . $e->getMessage();
}
?>